package com.ks.app.admin.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.xml.DOMConfigurator;

public class AdminFilter extends HttpServlet implements javax.servlet.Filter {
	
	private static final long serialVersionUID = 126151414508107785L;
	
	@SuppressWarnings("unused")
	private FilterConfig filterConfig;

	public void destroy() {

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain cahin) throws IOException, ServletException {
		
		HttpServletRequest req = (HttpServletRequest) request;
		HttpSession ses = req.getSession();
		String url = req.getRequestURI();//
		
		
		try {
			if (url.lastIndexOf("login") <= 0) {//拦截地址中不存在login的，判断session，还有那些不用拦截的可以追加上
				if (ses.getAttribute("merRegist") == null
						|| ses.getAttribute("merRegist").equals("")) {	
					response.setContentType("text/html");
					PrintWriter out = response.getWriter();
					out.print("<script>");
					out.print("top.location.href='/index.jsp';");
					out.print("</script>");	
					
				}else{
					cahin.doFilter(request, response);	
				}
			}else {		
				cahin.doFilter(request, response);				
			}	
		} catch (Exception e) {
			
		}
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		
		this.filterConfig = filterConfig;
	}

}
